//
//  faceIDViewController.m
//  dongfengTest
//
//  Created by wxt on 2021/3/16.
//

#import "FaceIDViewController.h"
#import <LocalAuthentication/LocalAuthentication.h>
#import "HomeViewController.h"
#import "LoginPassWordViewController.h"
@interface FaceIDViewController ()
@property (retain,nonatomic) LAContext *faceIDContext;

//@property (assign,nonatomic) NSInteger BiometryTypeNumber;

@end

@implementation FaceIDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = Wcolor;
    
    
    [self addUIView];
    
    

    
    //

    // Do any additional setup after loading the view.
}

-(void)addUIView{
    
    
    TKLlable * titLab = [TKLlable labelWithFrame:CGRectMake(50, kStatusBarHeight+10, kWidth-100, 20) text:[NSString stringWithFormat:@"%@支持登录",self.BiometryTypeStr] textColor:Bcolor font:Font_6(16) textAlignment:Textcenter backgroundColor:Ccolor];
    [self.view addSubview:titLab];
    titLab.font = [UIFont fontWithName:@"Arial-BoldMT" size:16];
  
    UIView * linView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(titLab.frame)+10, kWidth, 1)];
    linView.backgroundColor = JhColor(216, 216, 216);
    [self.view addSubview:linView];
  

    tklImgView * faceImg = [tklImgView imgViewWithFrame:CGRectMake(kWidth/2-60, CGRectGetMaxY(titLab.frame)+100, 120, 120) img:@""];
    [self.view addSubview:faceImg];
    
    TKLlable * messLab = [TKLlable labelWithFrame:CGRectMake(20, CGRectGetMaxY(faceImg.frame)+25, kWidth-40, 18) text:[NSString stringWithFormat:@"可开通%@识别功能",self.BiometryTypeStr] textColor:Bcolor font:Font_6(14) textAlignment:Textcenter backgroundColor:Ccolor];
    [self.view addSubview:messLab];
    
    TKLlable * signLab = [TKLlable labelWithFrame:CGRectMake(kWidth/2-70, CGRectGetMaxY(messLab.frame)+15, 140, 35) text:[NSString stringWithFormat:@"可通过验证系统%@\n快速完成登录",self.BiometryTypeStr] textColor:JhColor(153, 153, 153) font:Font_6(14) textAlignment:Textcenter backgroundColor:Ccolor];
    signLab.numberOfLines = 2;
    [self.view addSubview:signLab];
    
    UIButton * openBioBtn = [UIButton createBtnFrame:CGRectMake(30, CGRectGetMaxY(signLab.frame)+25, kWidth-60, 44) title:[NSString stringWithFormat:@"开通%@识别",self.BiometryTypeStr] bgImageName:@"" action:^(UIButton *button) {
        //开始识别
        
        [self   biometryIdAction];
        
        
        
    }];
    
    tklCAGradientLayer * glr = [tklCAGradientLayer layerWithFrame:openBioBtn.bounds startColorR:79 startColorG:151 startColorB:248 endColorR:45 endColorG:134 endColorB:243 ];
    
    [openBioBtn.layer insertSublayer:glr atIndex:0];
    [self.view addSubview:openBioBtn];
    
    
    UIButton * getOutBtn = [UIButton createBtnFrame:CGRectMake(30, CGRectGetMaxY(openBioBtn.frame)+15, kWidth-60, 44) title:@"跳过" bgImageName:@"" textcolor:Bcolor laycolor:Bcolor action:^(UIButton *button) {
        
        
        if ([self.pushPath isEqualToString:@"boi"]) {
            LoginPassWordViewController * vc = [[LoginPassWordViewController alloc]init];
            vc.modalPresentationStyle = 0;
            vc.userName = self.userName;
            [self presentViewController:vc animated:YES completion:nil];
        }else{
//            NSString *IDFV = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
            
//            [QiKeychainItem saveKeychainWithService:QikeychainService account:@"userUUID" password:IDFV];
//            [self  insterDataList];
            HomeViewController * tab = [[HomeViewController alloc]init];
            
            tab.modalPresentationStyle = 0;
            [self presentViewController:tab animated:YES completion:nil];
        }
        
      
        
        
        
        
        
    }];
    [self.view addSubview:getOutBtn];
    
    TKLlable * signMesLab = [TKLlable labelWithFrame:CGRectMake(30, CGRectGetMaxY(getOutBtn.frame)+25, kWidth-60, 20) text:@"后续可在我的-登录设置中开始或关闭" textColor:JhColor(153, 153, 153) font:Font_6(14) textAlignment:Textcenter backgroundColor:Ccolor];
    [self.view addSubview:signMesLab];
    
    
    if (self.BiometryTypeNumber == 1) {
        //指纹
        faceImg.image = [UIImage imageNamed:@"zhiwen"];
 
      
    }else if(self.BiometryTypeNumber == 2){
        //面部
        faceImg.image = [UIImage imageNamed:@"renlianshibie"];
   
    }else{
        faceImg.image = [UIImage imageNamed:@"renlianshibie"];
    }
    
    
    
    
}

-(void)biometryIdAction{

 
    
    NSString *localizedReason = [NSString stringWithFormat:@"%@识别",self.BiometryTypeStr];
    [self.faceIDContext evaluatePolicy:(LAPolicyDeviceOwnerAuthentication) localizedReason:localizedReason reply:^(BOOL success, NSError * _Nullable error) {
        if (success) {
            

            NSUserDefaults * stand = [NSUserDefaults standardUserDefaults];

                [NetRequestClass NetRequestPOSTWithRequestURL:kUser_UserDevice_Api WithParameter:@{@"deviceId":[IMSecurity getDeviceIdentifier],@"uno":self.userName,@"fpSwitch":@1} WithReturnValeuBlock:^(id  _Nonnull returnValue) {

                    if ([returnValue[@"code"] intValue] == 200) {
                        
                        NSUserDefaults * stand = [NSUserDefaults standardUserDefaults];
                
                        [stand setObject:@"1" forKey:@"bio"];
                        
                        if ([self.pushPath isEqualToString:@"login"]) {
                            HomeViewController * homeVC = [[HomeViewController alloc]init];
                            homeVC.modalPresentationStyle = 0;
                            [self presentViewController:homeVC animated:YES completion:nil];
                        }else{
                            [self dismissViewControllerAnimated:YES completion:nil];
                        }
                        
                       
         
                    }else{
                        [MBProgressHUD showError:@"绑定设备失败" toView:self.view];
                    }
    
                    
                } WithErrorCodeBlock:^(id  _Nonnull errorCode) {
                   NSLog(@"%@",error) ;
                } WithFailureBlock:^{
                    ;
                }];
            
            
        }else{
            NSLog(@"check error --->%@",error.localizedDescription);
            
            switch (error.code) {
                case LAErrorSystemCancel:
                    [MBProgressHUD showError:@"系统取消授权" toView:self.view];
                    break;
                case LAErrorUserCancel:
                    [MBProgressHUD showError:@"用户取消验证" toView:self.view];
                    break;
                case LAErrorAuthenticationFailed:
            
                    [MBProgressHUD showError:@"授权失败" toView:self.view];
                    break;
                case LAErrorPasscodeNotSet:
                    [MBProgressHUD showError:@"系统未设置密码" toView:self.view];
                    break;
                case LAErrorBiometryNotAvailable:
                    
                    [MBProgressHUD showError:@"设备生物ID不可用" toView:self.view];
            
                    
                    break;
                case LAErrorBiometryNotEnrolled:
                
                    [MBProgressHUD showError:@"设备生物ID不可用，用户未登录" toView:self.view];
                    break;
                case LAErrorUserFallback:{
                    
                    [[NSOperationQueue mainQueue]addOperationWithBlock:^{
                        NSLog(@"用户选择输入密码，切换到主线程");                 ;
                    }];
                    
                }
                    
                    break;
                default:{
                    
                    [[NSOperationQueue mainQueue]addOperationWithBlock:^{
                        NSLog(@"其他情况，切换到主线程");                 ;
                    }];
                    
                }
                    break;
            }
            
        }
    }];
//    self.faceIDContext = nil;
    
}
- (LAContext *)faceIDContext{
    if (!_faceIDContext) {
        _faceIDContext = [[LAContext alloc] init];
    }
    return _faceIDContext;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
